<?php
$loader->requireOnce('ordo/TicketItem.class.php');
$loader->requireOnce('ordo/Ticket.class.php');
$loader->requireOnce('ordo/QueueItem.class.php');
$loader->requireOnce('includes/AjaxRender/TicketRender.class.php');
$loader->requireOnce('includes/PatientQueue/AbsQueue.class.php');
$loader->requireOnce('includes/ParacliniqueEngine.class.php');


class C_TicketItem extends Controller {
	
	var $template_mod;					
	
	function C_TicketItem($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'TicketItem') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'patient';
		}
		$this->template_mod = $template_mod;			
		$this->view->path = 'ticket_item';
	}
						
	public function actionDeleteAjax() {
		$ticketItemId = $_GET['ticket_item_id'];
		$item = Celini::newORDO('TicketItem');
		if ($item->isExist($ticketItemId)) {
			$item->setup($ticketItemId);
			$item->delete($ticketItemId);
			return true;
			// temporary enable all to be deleted
			/*
			if ($item->get('status') != TicketItem::CLOSED) {
				$item->delete($ticketItemId);
				return true;
			}*/						
		}				
		return false;
	}
	
	public function actionPrePaidAjax() {
		$ticketItemId = $_GET['ticket_item_id'];
		$amount = $_GET['amount'];
		$item = Celini::newORDO('TicketItem');
		$prePaid = $item->prePaid($ticketItemId, $amount);	
		return $prePaid;
	}
	
	public function actionTestDetailAjax() {
		$ticketTestId = $_GET['ticket_test_id'];						
		return TicketRender::renderHTMLTest($ticketTestId);		
	}
	
	public function actionCompositeServiceDetailAjax() {
		$ticketItemId = $_GET['ticket_item_id'];						
		return TicketRender::renderHTMLCompositeService($ticketItemId);		
	}
	
	public function actionCompleteAjax() {
		$ticketItemId= $_GET['ticket_item_id'];				
		
		// get first open ticketItem in the current ticket
		$ticketItem = Celini::newORDO('TicketItem', $ticketItemId);
		
		// close current ticket item
		$ticketItem->changeStatus($ticketItemId, TicketItem::CLOSED);
		$ticketItem->close($ticketItemId);
		
		// get additional info of ticket
		$ticketId = $ticketItem->get('ticket_id');				
		$ticket = Celini::newORDO('Ticket');
		$firstOpenTicketItemId = $ticket->getFirstTicketItem($ticketId, TicketItem::OPEN);		
		$firstWaitingTicketItemId = $ticket->getFirstTicketItem($ticketId, TicketItem::WAITING);				
		
		$ticket->setup($ticketId);
		$status = $ticket->get('status');
		
		// record the next ticket_item_id 
		$nextTicketItemId = 0;
				
		// there is no more OPEN, IN_PROGRESS and WAITING ticket item
		// move to cashier queue  
		if ($firstOpenTicketItemId === false && $firstWaitingTicketItemId === false) {			
			// someone has moved this patient to cashier queue 
			// 1. If cashier hasn't collected money yet - moving patient will replace the previous one
			// 2. If cashier collected money, do not move to cashier queue
			if ($status != Ticket::CLOSED_STATUS) {
				$nextTicketItemId = $ticketItemId;	
				AbsQueue::moveTicketItemToQueue($ticketItemId, AbsQueue::CASHIER);
			}						
		} 		
		
		// Still having some OPENING ticket items
		// OPEN ticket item gets higher priority than the WAITING ones
		else if ($firstOpenTicketItemId !== false) {			
			$nextTicketItemId = $firstOpenTicketItemId;
			
			// remove current ticket item from queue_item				
			AbsQueue::removeTicketItem($ticketItemId);
				
			if ($ticketItem->isPaymentType($firstOpenTicketItemId, Service::BEFORE) == true) {							
				// Add new ticket item to Cashier queue	
				$queue = AbsQueue::getQueue(AbsQueue::CASHIER);
				$queue->addTicketItem($firstOpenTicketItemId);				
			} else {				
				// Add new ticket item to Encounter queue	
				$queue = AbsQueue::getQueue(AbsQueue::ENCOUNTER);
				$queue->addTicketItem($firstOpenTicketItemId);																
			}
		}
		
		// Still having some WAITING ticket items
		else if ($firstWaitingTicketItemId !== false) {
			$nextTicketItemId = $firstWaitingTicketItemId;
			AbsQueue::removeTicketItem($ticketItemId);																			
		}
		
		// get next room
		if ($nextTicketItemId == 0) {
			$roomName = "closed";
		} else {
			$queueItem = QueueItem::fromTicketItemId($nextTicketItemId);		
			$room = Celini::newORDO('Room', $queueItem->get('room_id'));
			$roomName = $room->getLocalizedText('name');
		}		
		$result = array('roomName' => $roomName,
			'redirectLink' => Celini::link('renderQueue', 'Queue'));	
				
		return json_encode($result);
	}
	
	public function actionWaitForParaClinicServiceAjax() {
		$ticketItemId= $_GET['ticket_item_id'];			
		
		// Move ticketItem to waiting queue
		AbsQueue::moveTicketItemToQueue($ticketItemId, AbsQueue::WAITING);
		
		// get first open ticketItem in the current ticket
		$ticketItem = Celini::newORDO('TicketItem', $ticketItemId);
		$ticketId = $ticketItem->get('ticket_id');				
		$ticket = Celini::newORDO('Ticket');
		$firstOpenTicketItemId = $ticket->getFirstTicketItem($ticketId, TicketItem::OPEN);
		$firstWaitingTicketItemId = $ticket->getFirstTicketItem($ticketId, TicketItem::WAITING);
		
		// record the next ticket_item_id 
		$nextTicketItemId = 0;
		
		// Still having some OPENING ticket items
		// OPEN ticket item gets higher priority than the WAITING ones
		if ($firstOpenTicketItemId !== false) {			
			$nextTicketItemId = $firstOpenTicketItemId;
			//start paraclinic assignment if it's paraclinic item
            
			if ($ticketItem->isPaymentType($firstOpenTicketItemId, Service::BEFORE) == true) {
				// Add new ticket item to Cashier queue
				$queue = AbsQueue::getQueue(AbsQueue::CASHIER);
				$queue->addTicketItem($firstOpenTicketItemId);				
			} else {				
				// Add new ticket item to Encounter queue	
				$queue = AbsQueue::getQueue(AbsQueue::ENCOUNTER);
				$queue->addTicketItem($firstOpenTicketItemId);																
			}
			 //start paraclinic assignment if it's paraclinic item
             //added by Viet Hoang
            $queueItem = QueueItem::fromTicketItemId($firstOpenTicketItemId);        
            ParacliniqueEngine::startParaClinicTicket($firstOpenTicketItemId,$queueItem->get('room_id'));
		} 
		
		// Find out the first waiting ticket item. There is at least one item, the current ticket item
		// is in the WAITING status
		else if ($firstWaitingTicketItemId !== false) {
			$nextTicketItemId = $firstWaitingTicketItemId;		
		}
		
		// get next room
		$queueItem = QueueItem::fromTicketItemId($nextTicketItemId);		
		$room = Celini::newORDO('Room', $queueItem->get('room_id'));
		$roomName = $room->getLocalizedText('name');			
		return json_encode(array(
			'roomName' => $roomName,
			'redirectLink' => Celini::link('renderQueue', 'Queue')));
	}
	
	public function actionContinueEncouterAjax() {
		$ticketItemId= $_GET['ticket_item_id'];		
		
		// Move ticketItem to waiting queue
		AbsQueue::moveTicketItemToQueue($ticketItemId, AbsQueue::IN_PROGRESS);
	}	 	
}
?>


















